package com.ar.datahandler;

import java.util.ArrayList;

import com.ar.bean.Exam;

import android.util.Log;

public class Examinator {
	private static float mark;
	private static int num_correct;
	private static ArrayList<String> lstDetail;

	private static int[] userAnswers;
	private static Exam data;
	private static int num_quiz;
	private static boolean[] ansFlag;
	private static int[] correctAnswers;

	public Examinator() {
		num_correct = 0;
		mark = 0;
		lstDetail = new ArrayList<String>();
	};

	public Examinator(int[] userAnswers, Exam exam) {
		num_correct = 0;
		mark = 0;
		lstDetail = new ArrayList<String>();
		this.userAnswers = userAnswers;
		data = exam;
	}

	public float getMark() {
		return mark;
	}

	public void setMark(float mark) {
		this.mark = mark;
	}

	public int getNumberOfcorrectAnswer() {
		return num_correct;
	}

	public void setNumberOfcorrectAnswer(int num_correct) {
		this.num_correct = num_correct;
	}

	/**
	 * @return the num_quiz
	 */
	public int getNumberOfquiz() {
		return num_quiz;
	}

	/**
	 * @param num_quiz
	 *            the num_quiz to set
	 */
	public void setNumberOfquiz(int num_quiz) {
		Examinator.num_quiz = num_quiz;
	}

	public void checkPoint(int pointScale) {
		try {
			num_quiz = data.getContent().size();
			ansFlag = new boolean[num_quiz];
			correctAnswers = new int[num_quiz];
			float aQuizPoint = (float) pointScale / num_quiz;
			Log.d("point", ""+aQuizPoint);
			for (int i = 0; i < num_quiz; i++) {
				String userAnswer = toWord(userAnswers[i]);
				correctAnswers[i] = data.getContent().get(i).getCorrectAnswer();
				String correctAnswer = toWord(correctAnswers[i]);
				if (correctAnswer.equals(userAnswer)) {
					lstDetail.add("<b><u><font color='#04bd84'>" + (i + 1)
							+ "</font></u>" + ". " + correctAnswer + "</b>");
					mark = num_correct * aQuizPoint;
					num_correct++;
					ansFlag[i] = true;
				} else {
					lstDetail.add("<b><u><font color='#04bd84'>" + (i + 1)
							+ "</font></u>" + ". " + userAnswer + "-> "
							+ "<font color='#dc5d54'>" + correctAnswer
							+ "</font></b>");
					ansFlag[i] = false;
				}
			}
		} catch (IllegalArgumentException e) {
			Log.e("IllegalArgumentException", "Argument 'divisor' is 0");
		}
	}

	/**
	 * @return the lstDetail
	 */
	public ArrayList<String> getListDetail() {
		return lstDetail;
	}

	/**
	 * @param lstDetail
	 *            the lstDetail to set
	 */
	public void setListDetail(ArrayList<String> lstDetail) {
		this.lstDetail = lstDetail;
	}

	private static String toWord(int answer_id) {
		switch (answer_id) {
		case 0:
			return "A";
		case 1:
			return "B";
		case 2:
			return "C";
		case 3:
			return "D";
		case 4:
			return "E";
		case 5:
			return "F";
		case 6:
			return "G";
		case 7:
			return "H";
		case 8:
			return "I";
		case 9:
			return "J";
		case 10:
			return "K";
		default:
			return "";
		}
	}

	/**
	 * @return the ansFlag
	 */
	public boolean[] getAnsFlag() {
		return ansFlag;
	}

	/**
	 * @param ansFlag
	 *            the ansFlag to set
	 */
	public void setAnsFlag(boolean[] ansFlag) {
		Examinator.ansFlag = ansFlag;
	}

	/**
	 * @return the correctAnswers
	 */
	public int[] getCorrectAnswers() {
		return correctAnswers;
	}

	/**
	 * @param correctAnswers the correctAnswers to set
	 */
	public void setCorrectAnswers(int[] correctAnswers) {
		Examinator.correctAnswers = correctAnswers;
	}

}
